package com.aaa.controller;

import com.aaa.pojo.UserInfo;
import com.aaa.service.UserService;
import com.aaa.tool.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class UserController {
    @Autowired
    private UserService service;

    @Resource
    private PasswordEncoder passwordEncoder;

    @RequestMapping("/updatePwd")
    public Result updatePwdByUsername(HttpServletRequest request, String oldPwd,String password){
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        String oriPassword = service.selPwdByUid(uid);
        String newPwd=passwordEncoder.encode(password);
        System.out.println(uid);
        System.out.println(oldPwd);
        System.out.println(password);
        System.out.println(oriPassword);
        System.out.println(newPwd);
        if (!passwordEncoder.matches(oldPwd,oriPassword)){
            System.out.println("密码不一致");
            return Result.error("密码不一致");
        }else{
            service.updatePwdByUid(uid,newPwd);
            System.out.println("修改成功");
            return Result.ok();
        }
    }
}
